package com.timothy.asset.data;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 *
 * @author applerockhool
 * @version 2010-4-29
 */
public class MarketPriceUtil {

    public static List<String> getSecurityInfoList(String securityCodes) {
        List<String> securityInfoList = new ArrayList<String>();

        String url = "http://hq.sinajs.cn/list=" + securityCodes;
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            try {
                in = u.openStream();
                int i;
                while ((i = in.read(b)) != -1) {
                    bo.write(b, 0, i);
                }
                String result = bo.toString();
                String[] securities = result.split(";");
                for (String security : securities) {
//                        System.out.println(security);
                    String security_info = security.substring(security.indexOf("\"") + 1, security.length() - 1);
                    System.out.println(security_info);
                    securityInfoList.add(security_info);
                }
                bo.reset();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return securityInfoList;
    }


    public static double getSingleStockPrice(String stockCode){
        double price = 0;

        String url = "http://hq.sinajs.cn/list="+stockCode;
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
                try {
                    in = u.openStream();
                    int i;
                    while ((i = in.read(b)) != -1) {
                        bo.write(b, 0, i);
                    }
                    //
                    String stockInfo = bo.toString();
                    String info = stockInfo.substring(stockInfo.indexOf("\"") + 1, stockInfo.length() - 1);
                        int seq=0;
                        String[] datas = info.split(",");
                        for (String data : datas) {
                            if(seq == 3){
                                return Double.valueOf(data);
                            }
                            seq++;
                        }
                        System.out.println("--------------------");
//                    }
                    bo.reset();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
//            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return price;
    }

    public static String getSingleSecurityData(String stockCode,int assign_seq){
        String url = "http://hq.sinajs.cn/list="+stockCode;
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            try {
                in = u.openStream();
                int i;
                while ((i = in.read(b)) != -1) {
                    bo.write(b, 0, i);
                }
                String stockInfo = bo.toString();
                String info = stockInfo.substring(stockInfo.indexOf("\"") + 1, stockInfo.length() - 1);
                int seq=0;
                String[] datas = info.split(",");
                for (String data : datas) {
                    if(seq == assign_seq){
                        return data;
                    }
                    seq++;
                }
                bo.reset();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return "";
    }


    public static String getExchangeData(String exchangeCode,int assign_seq){
        String url = "http://hq.sinajs.cn/rn=1318986628214&list="+exchangeCode;
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            try {
                in = u.openStream();
                int i;
                while ((i = in.read(b)) != -1) {
                    bo.write(b, 0, i);
                }
                String stockInfo = bo.toString();
                String info = stockInfo.substring(stockInfo.indexOf("\"") + 1, stockInfo.length() - 1);
                int seq=0;
                String[] datas = info.split(",");
                for (String data : datas) {
                    if(seq == assign_seq){
                        return data;
                    }
                    seq++;
                }
                bo.reset();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        return "";
    }
    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        MarketPriceUtil mp = new MarketPriceUtil();
        mp.getSecurityInfoList("sh600036,sh600016,sh600000,sz150018,sz111039");
    }
	
}

